<?php
/*************************************************

评论公共控制器 - the 
Author: Reload <17-12-12>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class CommentController extends Controller {

    public function get_comments($userid,$id_target,$page,$pagenum,$id=0){
       //查询评论
       if($id){
        $map['p_id'] = $id;
        $map['id_parent'] = $id;
       }else{
    //    $map['id'] = $id;
        $map['id_parent'] =0;
        $map['p_id'] = 0;
       }
        $map['status'] =0;
    //    $map['type'] =$type;
        $map['id_target'] =$id_target;
       $order = 'add_time desc';
    //   echo '*********'.$id.'*********';
    //   print_r($map);
       $r = D('Comment')->get_all_page($map,$field="*",$order,$page,$pagenum);
    //   echo D('Comment')->_sql();
    //   print_r($map);
    //   print_r($r);die;
       foreach($r as $k=>$v){
            $add_time = $v['add_time'];
            $long = time()-$add_time;
           
            if($long<60){
                $r[$k]['add_date'] = $long."秒前";
            }else if($long<3600){
                $r[$k]['add_date'] = ceil($long/60)."分钟前";
            }else if($long<86400){
                $r[$k]['add_date'] = ceil($long/86400)."天前";
            }else if($long>86400){
                $r[$k]['add_date'] = date('m-d H:i:s',$add_time);
            }else if($long=0){
                 $info['add_date'] = "刚刚";
            }

            //判断用户是否点赞
            $praise = 0;
            if($userid){
                $map = array();
                $map['id_user'] = $userid;
                $map['id_target'] =$v['id'];
                $map['type'] =3;
                $praise = D('Praise')->get_one($map);
                if($praise){
                    $praise = 1;
                }else{
                    $praise = 0;
                }
            }
            $r[$k]['praise'] = $praise;


            $map = array();
            $map['p_id'] =$v['id'];
            $r[$k]['comment_count'] = D('Comment')->count($map);
           
             foreach($r as $k=>$v){
                $p_user_info = get_user_info($v['user_id']);
                $r[$k]['user'] = $p_user_info;
                $id = $v['id'];
                $map = array();
                $map['p_id'] = $id;
                $map['status'] =0;
                $kid = D('Comment')->get_all($map,$field="*",$order);

                foreach($kid as $ke=>$va){
                        $g_user_info = get_user_info($va['user_id']);
                        $kid[$ke]['user'] = $g_user_info;

                        $map = array();
                        $map['id'] = $va['id_parent'];
                        $parent = D('Comment')->get_one($map);
                        $p_user_info = get_user_info($parent['user_id']);
                        $kid[$ke]['p_user'] = $p_user_info['nickname'];


                        //判断用户是否点赞
                        $praise_kid = 0;
                        if($userid){
                            $map = array();
                            $map['id_user'] = $userid;
                            $map['id_target'] =$v['id'];
                            $praise_kid = D('Praise')->get_one($map);
                            if($praise_kid){
                                $praise_kid = 1;
                            }else{
                                $praise_kid = 0;
                            }
                        }
                        $kid[$ke]['praise'] = $praise_kid;

                        $add_time = $va['add_time'];
                        $long = time()-$add_time;

                         if($long<60){
                                 $kid[$ke]['add_date'] = $long."秒前";
                            }else if($long<3600){
                                 $kid[$ke]['add_date'] = ceil($long/60)."分钟前";
                            }else if($long<86400){
                                 $kid[$ke]['add_date'] = ceil($long/86400)."天前";
                            }else if($long<684800){
                                 $kid[$ke]['add_date'] = date('Y-m-d H:i:s');
                            }else if($long=0){
                                 $info['add_date'] = "刚刚";
                            }




                }

                if($kid){
                 $r[$k]["kid"] = $kid;
                }

             }
           //print_r($r);die; 
       }
        return  $r;
    }



    //更新获取单条评论数据
    public function get_one_comment(){
        $id = I('post.id');
        $map['id'] = $id;
        $info = D('Comment')->get_one($map);

        $add_time = $info['add_time'];
        $long = time()-$add_time;
            if($long<60){
                 $info['add_date'] = $long."秒前";
            }else if($long<3600){
                 $info['add_date'] = ceil($long/60)."分钟前";
            }else if($long<86400){
                 $info['add_date'] = ceil($long/86400)."天前";
            }else if($long<684800){
                 $info['add_date'] = date('Y-m-d H:i:s');
            }else if($long=0){
                 $info['add_date'] = "刚刚";
            }

        $user_info = get_user_info($info['user_id']);
        $info['user'] = $user_info;
        $this->assign('info',$info);
        $content = $this->fetch('News/comment_item');
        $r = array("content" =>$content);
        $this->ajaxReturn($r);

    }
//更新获取单条回复数据
    public function get_one_reply(){
        $id = I('post.id');
        $map['id'] = $id;
        $info = D('Comment')->get_one($map);
        $add_time = $info['add_time'];
        $long = time()-$add_time;
            if($long<60){
                 $info['add_date'] = $long."秒前";
            }else if($long<3600){
                 $info['add_date'] = ceil($long/60)."分钟前";
            }else if($long<86400){
                 $info['add_date'] = ceil($long/86400)."天前";
            }else if($long<684800){
                 $info['add_date'] = date('Y-m-d H:i:s');
            }else if($long=0){
                 $info['add_date'] = "刚刚";
            }

        $user_info = get_user_info($info['user_id']);
        $info['user'] = $user_info;

        $map = array();
        $map['id'] = $info['id_parent'];
        $parent = D('Comment')->get_one($map);
        $p_user_info = get_user_info($parent['user_id']);
        $info['p_user'] = $p_user_info['nickname'];
        $this->assign('info',$info);
        $content = $this->fetch('News/reply_item');
        $r = array("content" =>$content);
        $this->ajaxReturn($r);
    }

    //发表评论
/*    public function add_coments(){
        $data = I('post.');
        if($data['p_id']) {
            $data['id_parent'] = $data['p_id'];
        }
    //    print_r($data);die;
        header('content-type:Text/html; charset=utf-8');
        $content=file_get_contents("App/Home/Public/mingan.txt");
        $arr=explode("\r\n",$content);
        $badword1 = array_combine($arr,array_fill(0,count($arr),'***'));
        //print_r($this->strtr_array($data['content'], $badword1))  ;die;
        $content = $this->strtr_array($data['content'], $badword1);
        if($content['rps']==1){
            $data = array('errorCode'=>3,'msg'=>"请文明留言，严禁发布各类敏感不实广告信息！");
            $this->ajaxReturn($data);
        }
        $data['content'] = I('post.content');
        $data['add_time'] = time();
        $data['user_id'] = I('post.userid');
        $map['id'] = I('post.userid');
        $user  = D('User')->get_one($map);
        $comment_status= $user['comment_status'];
        if($comment_status==1){
            $data = array('errorCode'=>3,'msg'=>"您已经被禁言,发表失败！");
             $this->ajaxReturn($data);
        }
      //  print_r($data);die;
        if($user['card_status']==1) {
            $r = D('Comment')->add($data);
                if($r){
                    //增加评论数
                    $type = $data['type'];
                    if($type==1){
                        $map =array();
                        $map['id'] = $data['id_target'];
                        $res = D('NewsHouse')->set_read_int($map);
                    }else if($type==2){
                        $map =array();
                        $map['id'] = $data['id_target'];
                        $res = D('Video')->set_comments_int($map);
                    }
                    $data = array('errorCode'=>1,'msg'=>"发表成功！",'id'=>$r);
                }else{
                    $data = array('errorCode'=>0,'msg'=>"发表失败！");
                }
        } else {
            $data = array('errorCode'=>2,'msg'=>"未实名，请先实名！");
        }

        $this->ajaxReturn($data);
    }*/


    //发表评论
    public function add_coments(){
        $data = I('post.');
        header('content-type:Text/html; charset=utf-8');
        $content=file_get_contents("App/Home/Public/mingan.txt") ;
        $arr=explode("\r\n",$content);
        $badword1 = array_combine($arr,array_fill(0,count($arr),'***'));
        //print_r($this->strtr_array($data['content'], $badword1));die;
        $content = $this->strtr_array($data['content'], $badword1);
        if($content['rps']==1){
            $data = array('errorCode'=>2,'msg'=>"请文明留言，严禁发布各类敏感不实广告信息！");
            $this->ajaxReturn($data);
        }
        $data['content'] = I('post.content');
        $data['add_time'] = time();
        $data['user_id'] = I('post.userid');
        $comment_status= I('post.comment_status');
        if($comment_status==1){
            $data = array('errorCode'=>2,'msg'=>"您已经被禁言,发表失败！");
             $this->ajaxReturn($data);
        }
      //  print_r($data);die;
        $r = D('Comment')->add($data);
        $is_msg = 1;
            if($r){
                //增加评论数
                $type = $data['type'];
                if($type==1){
                    $map =array();
                    $map['id'] = $data['id_target'];
                    $res = D('News')->set_comments_int($map);
                    $is_msg = 0;
                    $typename = 'comment';
                }else if($type==2){
                    $map =array();
                    $map['id'] = $data['id_target'];
                    $res = D('Video')->set_comments_int($map);
                    $is_msg = 0;
                }else if($type==4){
                    $map =array();
                    $map['id'] = $data['id_target'];
                    $res = D('PlatformCommentary')->set_comment_int($map);
                    $i = D('PlatformCommentary')->get_one($map);
                    $send_id = $i['user_id'];
                    $type = 10;
                    $typename = 'dp_c';
                }else if($type==6){
                    $map =array();
                    $map['id'] = $data['id_target'];
                    $res = D('CircleOpinion')->set_comment_int($map);
                    $i = D('CircleOpinion')->get_one($map);
                    $send_id = $i['user_id'];
                    $type = 12;
                    $typename = 'option_c';
                }else if($type==5){
                    $map =array();
                    $map['id'] = $data['id_target'];
                    $res = D('Depth')->set_comment_int($map);
                    $i = D('Depth')->get_one($map);
                    $send_id = $i['user_id'];
                    $type = 11;
                    $typename = 'depth_c';
                }else if($type==7){
                    $map =array();
                    $map['id'] = $data['id_target'];
                    $res = D('CircleAnnounce')->set_comment_int($map);
                    $i = D('CircleAnnounce')->get_one($map);
                    $send_id = $i['user_id'];
                    $type = 13;
                    $typename = 'ann_c';
                }
                $user_id = I('post.userid');
                if($is_msg){
                    R('Msg/sendmail',array('typename'=>'comment','userid'=>$send_id,'val'=>1,'type'=>$type,'t_id'=>$data['id_target'],'t_user_id'=>$user_id,'content'=>0,'commentid'=>$r));
                }
                $data = array('errorCode'=>1,'msg'=>"发表成功！",'id'=>$r);
            }else{
                $data = array('errorCode'=>0,'msg'=>"发表失败！");
            }

        $this->ajaxReturn($data);
    }





//敏感词汇屏蔽
function strtr_array(&$str,&$replace_arr) {
    $maxlen = 0;$minlen = 1024*128;
    $rps=0;
    if (empty($replace_arr)) return $str;
    $str = preg_replace("/\s+/",'',$str);
    $char = "。、！？：；﹑•＂…‘’“”〝〞∕¦‖—　,^&*()!@#$%^〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔！¡？¿﹖﹌﹏﹋＇´ˊˋ―﹫︳︴¯＿￣﹢﹦﹤‐­˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼（）";
    $pattern = array(
    "/[[:punct:]]/i", //英文标点符号
    '/['.$char.']/u', //中文标点符号
    '/[ ]{2,}/'
    );
    $str = preg_replace($pattern,'',$str);
    foreach($replace_arr as $k => $v) {
        $len = strlen($k);
        if ($len < 1) continue;
        if ($len > $maxlen) $maxlen = $len;
        if ($len < $minlen) $minlen = $len;
    }
    $len = strlen($str);
    $pos = 0;$result = '';
    while ($pos < $len) {
        if ($pos + $maxlen > $len) $maxlen = $len - $pos; 
        $found = false;$key = '';
        for($i = 0;$i<$maxlen;++$i) $key .= $str[$i+$pos]; //原文：memcpy(key,str+$pos,$maxlen)
        for($i = $maxlen;$i >= $minlen;--$i) {
            $key1 = substr($key, 0, $i); //原文：key[$i] = '\0'
            if (isset($replace_arr[$key1])) {
                $result .= $replace_arr[$key1];
                $rps =1;
                $pos += $i;
                $found = true;
                break;
            }
        }
        if(!$found) $result .= $str[$pos++];
    }
    $data = array('result'=>$result,'rps'=>$rps);
    return $data;
}


 }